<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<link rel="stylesheet" type="text/css" href="../js/themes/default/peasyui.css">
	<link rel="stylesheet" type="text/css" href="../js/themes/icon.css">
	<link rel="stylesheet" type="text/css" href="../demo/demo.css">
	<link rel="stylesheet" type="text/css" href="../css/coun.css"/>
	<script type="text/javascript" src="../js/jquery-1.8.3.min.js"></script>
	<script type="text/javascript" src="../js/template.js"></script>
	<script type="text/javascript" src="../js/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../js/locale/easyui-lang-zh_CN.js"></script>
	<script type="text/javascript" src="../js/plugins/jquery.datagrid.js"></script>
	<script type="text/javascript">
				$(function(){
				
					loadTable();
				
			
				});	
				function loadTable(){
					//自定义1个combobox编辑器
					$.extend($.fn.datagrid.defaults.editors, {
					
						    combogrid: {			
						        init: function(container, options){			
						            var input = $('<input type="text" class="datagrid-editable-input">').appendTo(container);
						            input.combogrid(options);		
						            return input;			
						        },
					
						        destroy: function(target){			
						            $(target).combogrid('destroy');		
						        },
					
						        getValue: function(target){			
						            return $(target).combogrid('getValue');		
						        },
						
						        setValue: function(target, value){		
						            $(target).combogrid('setValue', value);			
						        },
						
						        resize: function(target, width){		
						            $(target).combogrid('resize',width);			
						        }
						
						    }
						
						});

					
					
					var ds =[
				{

				"ystype": "1",
				"ysname": "应收"
				},
				{
					"ystype": "0",
					"ysname": "应付"
				}
				];
					
					var as="";
					var xiaofeishuju ="";
					
						$.ajax({
							type:"GET",
					 		async: false,
							url:'<%=path%>/main/get/subinfo',
							dateType:'json',
							data:null,
							success:function(data){
								var result = eval('(' + data + ')');
								as=result.data;
							
								}
							});
						
						$.ajax({
							type:"GET",
					    	async: false,
					   	 	url:"<%=path %>/main/get/xiaofei",
					   		data :null,
					   		dataType: "json",
					    	success:function(data){
					    		console.log(data);					    		
					    		xiaofeishuju = data.data;		    		
						    	
					    	}
						});
						
						
					
					$('#tt').datagrid({
						
						width:924,
						height:350,
						singleSelect:true,
						rownumbers:true,
						selectOnCheck: true,
					    checkOnSelect: true,
						title:'',	
						columns:[[
						{field:'id',title:'序号',width:190,checkbox:true},
			            {  
			                field: 'xiaofei',  
			                title: '消费大类',  
			                width: 100,  
			                formatter: function(value) {  
			                    for (var i = 0; i < xiaofeishuju.length; i++) {  
			                        if (xiaofeishuju[i].code == value){ 
			                        	
			                        	return xiaofeishuju[i].codename
			                        };  
			                    }  
			                    return value;  
			                },  
			                editor: {  
			                    type: 'combobox',  
			                    options: {  
			                        valueField: 'code',  
			                        textField: 'codename',  
			                        data: xiaofeishuju,  
			                        required: true,
			                        onSelect:function(rowIndex, rowData){
			                  
			                        	var row = $("#tt").datagrid('getSelected');
			                        	var rindex = $("#tt").datagrid('getRowIndex', row);
			                   
			                        	var ed = $("#tt").datagrid('getEditor', {
			                                index: rindex,
			                                field: 'xiaofei'
			                            });
			                        	  var s = $(ed.target).combobox('getText');
			                        
			                        	$.ajax({
			                				type:"GET",
			                		 		async: false,
			                				url:'<%=path%>/main/get/zixiaofei',
			                				dateType:'json',
			                				data:{"iname":s},
			                				success:function(data){
			                					
			                					var result = eval('(' + data + ')');
			              					
			                					console.log(result.data);
			                					var editor = $("#tt").datagrid("getEditor",{index:rindex,field:'xiangmu'});
			                					$(editor.target).combobox("loadData",result.data);
			                			
			                					}
			                				});
			                        	}
			                    }  
			                }  
			            },
			            {  
			                field: 'xiangmu',  
			                title: '项目',  
			                width: 100,  
			                formatter: function(value) {  
			                    for (var i = 0; i < xiaofeishuju.length; i++) {  
			                        if (xiaofeishuju[i].code == value){ 
			                        	
			                        	return xiaofeishuju[i].codename
			                        };  
			                    }  
			                    return value;  
			                },  
			                editor: {  
			                    type: 'combobox',  
			                    options: {  
			                        valueField: 'code',  
			                        textField: 'codename',  
			                        data: xiaofeishuju,  
			                        required: true,			  
			                    }  
			                }  
			            },
			            {field:'mingxi',title:'项目明细',width:195,editor: { type: 'text', options: { required: true } }},
						{field:'shoushudc1',title:'手术医师',name:'shoushudc',width:100,            	
            	            editor:{            	
            	                type:'combogrid',           	
            	                options:{            
            	                    panelWidth:200,            	
            	                    idField:'cPersonCode',            
            	                    textField:'cPersonName',          
            	                    url:'<%=path%>/main/get/persons',  
            	                    columns:[[
            	                        {field:'cDepCode',title:'部门id',width:60,checkbox:true},
            	                        {field:'cPersonCode',title:'职员编码',width:60},           
            	                        {field:'cPersonName',title:'职员姓名',width:100},
            	                    ]]
            
            	                }
            
            	            }
            	        },
            			{field:'shoushudc2',title:'配台医师',name:'shoushudc',width:100,            	
            	            editor:{            	
            	                type:'combogrid',           	
            	                options:{            
            	                    panelWidth:200,            	
            	                    idField:'cPersonCode',            
            	                    textField:'cPersonName',          
            	                    url:'<%=path%>/main/get/persons',  
            	                    columns:[[
            	                        {field:'cDepCode',title:'部门id',width:60,checkbox:true},
            	                        {field:'cPersonCode',title:'职员编码',width:60},           
            	                        {field:'cPersonName',title:'职员姓名',width:100},
            	                    ]]
            
            	                }
            
            	            }
            	        },
            			{field:'shoushudc3',title:'非手术医师',name:'shoushudc',width:100,            	
            	            editor:{            	
            	                type:'combogrid',           	
            	                options:{            
            	                    panelWidth:200,            	
            	                    idField:'cPersonCode',            
            	                    textField:'cPersonName',          
            	                    url:'<%=path%>/main/get/persons',  
            	                    columns:[[
            	                        {field:'cDepCode',title:'部门id',width:60,checkbox:true},
            	                        {field:'cPersonCode',title:'职员编码',width:60},           
            	                        {field:'cPersonName',title:'职员姓名',width:100},
            	                    ]]
            
            	                }
            
            	            }
            	        },
            			{field:'shoushudc4',title:'医师助手',name:'shoushudc',width:100,            	
            	            editor:{            	
            	                type:'combogrid',           	
            	                options:{            
            	                    panelWidth:200,            	
            	                    idField:'cPersonCode',            
            	                    textField:'cPersonName',          
            	                    url:'<%=path%>/main/get/persons',  
            	                    columns:[[
            	                        {field:'cDepCode',title:'部门id',width:60,checkbox:true},
            	                        {field:'cPersonCode',title:'职员编码',width:60},           
            	                        {field:'cPersonName',title:'职员姓名',width:100},
            	                    ]]
            
            	                }
            
            	            }
            	        },
            	        {field:'biaojia',title:'标价',width:195,editor: { type: 'text', options: { required: true } }},
            	        {field:'shuliang',title:'数量',width:195,editor: { type: 'text', options: { required: true } }},
            	        {field:'zhekou',title:'折扣',width:195,editor: { type: 'text', options: { required: true } }},
            	        {field:'zhehoupr',title:'折后价格',width:195,editor: { type: 'text', options: { required: true } }},
            	        {field:'beizhu',title:'备注',width:195,editor: { type: 'text', options: { required: true } }},	
				        {field:'action',title:'Action',width:90,align:'center',
			                formatter:function(value,row,index){
			                    if (row.editing){
			                        var s = '<a onclick="saverow(this)">Save</a> ';
			                        var c = '<a href="#" onclick="cancelrow(this)">Cancel</a>';
			                        return s+c;
			                    } else {
			                        var e = '<a href="#" onclick="editrow(this)">Edit</a> ';
			                        var d = '<a href="#" onclick="deleterow(this)">Delete</a>';
			                        return e+d;
			                    }
			                }
			            }
						          ]],
						          onBeforeEdit:function(index,row){
							            row.editing = true;
							            updateActions(index);
							        },
							        onAfterEdit:function(index,row){
							            row.editing = false;
							            updateActions(index);
							        },
							        onCancelEdit:function(index,row){
							            row.editing = false;
							            updateActions(index);
							        }
					});	
				
					
					
					//点击 getcheck按钮获取  选中checkbox的一行值
					$('#btn1').click(function(){
						
						var checkedItems = $('#tt').datagrid('getChecked');
						var names = [];
						$.each(checkedItems, function(index, item){
							names.push(item.settlementcode);
							names.push(item.settlement);
							names.push(item.subcode);
							names.push(item.subname);
							});
						var sde=names[0];
						var st=names[1];
						var sb=names[2];
						var sn=names[3];
						
						console.log(sde+","+st+","+sb+","+sn);
						
						$.ajax({
							type:"GET",
					 		async: false,
							url:'<%=path%>/st/insert/insertSettleStyle',
							dateType:'json',
							data:{"sde":sde,"st":st,"sb":sb,"sn":sn},
							success:function(data){
								var result = eval('(' + data + ')');
								var num = result.status;
								if(num!=0){
									alert(1);
									}
								}
							});
						
						});
				}
				function updateActions(index){
				    $('#tt').datagrid('updateRow',{
				        index: index,
				        row:{}
				    });
				}
				
				function getRowIndex(target){
				    var tr = $(target).closest('tr.datagrid-row');
				    return parseInt(tr.attr('datagrid-row-index'));
				}
				function editrow(target){
				    $('#tt').datagrid('beginEdit', getRowIndex(target));
				}
				function deleterow(target){
				    $.messager.confirm('Confirm','Are you sure?',function(r){
				        if (r){
				            $('#tt').datagrid('deleteRow', getRowIndex(target));
				        }
				    });
				}
				function saverow(target){
				    $('#tt').datagrid('endEdit', getRowIndex(target));
				}
				function cancelrow(target){
				    $('#tt').datagrid('cancelEdit', getRowIndex(target));
				}
				
				function insert(){
					var row = $('#tt').datagrid('getSelected');
					if (row){
						var index = $('#tt').datagrid('getRowIndex', row);
					} else {
						index = 0;
					}
					$('#tt').datagrid('insertRow', {
						index: index,
						row:{
							status:'P'
						}
					});
					$('#tt').datagrid('selectRow',index);
					$('#tt').datagrid('beginEdit',index);
				}
	</script>


</head>

<body>

<div>
	<div style="margin-left:0px" id="dd1">
			<a href="javascript:void(0)" class="easyui-linkbutton l-btn l-btn-small" onclick="insert()">
				<span class="l-btn-left">
					<span class="l-btn-text">Insert row</span>
				</span>
			</a>
		</div>
		<table id="tt">
			
		</table>
	<div class="combined"><label>合计</label><input type="text" value="" /></div>
</div>

</body>
</html>
    